package com.zjh.common.exception;


import com.zjh.common.entity.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import static com.zjh.common.entity.ResponseCode.BAD_REQUEST;
import static com.zjh.common.entity.ResponseCode.INTERNAL_SERVER_ERROR;

/**
 * @author shanhuiming
 */
@RestControllerAdvice
@Slf4j
public class ExceptionAdvice {


    @ExceptionHandler(IllegalArgumentException.class)
    public Response<Void> handlerIllegalArgumentException(IllegalArgumentException e) {
        log.error("参数非法：", e);
        Response<Void> resp = Response.error(BAD_REQUEST, e.getMessage());
        return resp;
    }

    @ExceptionHandler(Exception.class)
    public Response<Void> handlerException(Exception e) {
        log.error("", e);
        Response<Void> resp = Response.error(INTERNAL_SERVER_ERROR, e.getMessage());
        return resp;
    }

    @ExceptionHandler(ServiceException.class)
    public Response<Void> handlerServiceException(ServiceException e) {
        log.error("", e);
        Response<Void> resp = Response.error(INTERNAL_SERVER_ERROR, e.getMessage());
        return resp;
    }

    @ExceptionHandler(BadCredentialsException.class)
    public Response<Void> handleBadCredentialsException(BadCredentialsException e) {
        log.error("", e);
        Response<Void> resp = Response.error(INTERNAL_SERVER_ERROR, e.getMessage());
        return resp;
    }
}
